//
//  RemoteControllerWrapper.m
//  MultiScreens
//
//  Created by apple on 15-1-12.
//  Copyright (c) 2015年 skyworth.digital. All rights reserved.
//

#import "RemoteControllerWrapper.h"
//#import "Platinum.h"
//#import "SK_RemouteControler.h"
#import <Platinum/SK_RemouteControler.h>
#import <Platinum/SK_Platinum.h>
#import "DlnaKeyboardEventData.h"
#import "FunctionUtility.h"
@implementation RemoteControllerWrapper{

}

+(RemoteControllerWrapper*)sharedRC{
    static RemoteControllerWrapper *sharedRC = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedRC = [[[self class] alloc] init];
    });
    return sharedRC;

}


-(void)start{
    PltRemouteControlerInit();
}

-(void)stop{
    PltRemouteControlerStop();
}


/**
 *  遥控器server的回调
 *
 *  @param actionName
 *  @param NPT_String
 *  @param value
 */

void remoteCtlerActionCallAppIOS(NPT_String & actionName, NPT_Map < NPT_String, NPT_String > & value){
    NSLog(@"fun %s  ",__FUNCTION__);
    
    if (strcmp(actionName, "OnRCSAdded")==0) {
        NPT_String *frindlyName;
        value.Get("FriendlyName", frindlyName);
        
        NSString *nsFriendlyName =  [NSString stringWithCString:*frindlyName encoding:NSUTF8StringEncoding];
        frindlyName = NULL;
        NPT_String *uuid;
        value.Get("Uuid", uuid);
        
        NSString *nsUuid = [NSString stringWithCString:*uuid encoding:NSUTF8StringEncoding];
        uuid = NULL;
        NPT_String *type;
        value.Get("Type", type);
        NSString *nsType = [NSString stringWithCString:*type encoding:NSUTF8StringEncoding];
        type = NULL;
        NPT_String *descriptionUrl;
        value.Get("DescriptionUrl", descriptionUrl);
        NSString *nsDescriptionUrl = [NSString stringWithCString:*descriptionUrl encoding:NSUTF8StringEncoding];
        descriptionUrl = NULL;
        DlnaDevice *device = [[DlnaDevice alloc] initWithUuid:nsUuid FriendlyName:nsFriendlyName Type:nsType DescriptionUrl:nsDescriptionUrl];
    
        [[RemoteControllerWrapper sharedRC].delegate OnRCSAdded:device];
    }else if (strcmp(actionName, "OnRCSRemoved")==0){
        NPT_String *uuid;
        value.Get("Uuid", uuid);
        
        NSString *nsUuid = [NSString stringWithCString:*uuid encoding:NSUTF8StringEncoding];
        uuid = NULL;
        DlnaDevice *device = [[DlnaDevice alloc] initWithUuid:nsUuid FriendlyName:@"" Type:@"" DescriptionUrl:@""];
        [[RemoteControllerWrapper sharedRC].delegate OnRCSRemoved:device];
    
    }else if (strcmp(actionName, "ReportTouchScreenBoundaryResult")==0){
        NPT_String *boundary;
        value.Get("Boundary", boundary);
        
        NSString *nsBoundary  =[NSString stringWithCString:*boundary encoding:NSUTF8StringEncoding];
        nsBoundary = [nsBoundary stringByReplacingOccurrencesOfString:@"\n\r" withString:@""];
        
        NSRange range0 = [nsBoundary rangeOfString:@"Minx"];
        NSRange range1 = [nsBoundary rangeOfString:@"Maxx"];
        NSRange range2 = [nsBoundary rangeOfString:@"Miny"];
        NSRange range3 = [nsBoundary rangeOfString:@"Maxy"];
        
        NSInteger minx,miny,maxx,maxy;
        if (range0.length>0 && range1.length>0 && range2.length>0&& range3.length>0) {
            minx = [[nsBoundary substringWithRange:NSMakeRange(range0.location+range0.length, range1.location - range0.location - range0.length)] integerValue];
            maxx = [[nsBoundary substringWithRange:NSMakeRange(range1.location+range1.length, range2.location - range1.location - range1.length)] integerValue];
            miny = [[nsBoundary substringWithRange:NSMakeRange(range2.location+range2.length, range3.location - range2.location - range2.length)] integerValue];
            maxy = [[nsBoundary substringWithRange:NSMakeRange(range3.location+range3.length, nsBoundary.length - range3.location - range3.length)] integerValue];
        }
        
        CGRect bounds = CGRectMake(minx, miny, maxx, maxy);
        [RemoteControllerWrapper sharedRC].touchScreenBoundaryValue = [NSValue valueWithCGRect:bounds];
        NSLog(@"Boundary = %@",nsBoundary);
    }else if (strcmp(actionName, "ReportSetTouchEventResult") == 0){
        NPT_String *returnState;
        value.Get("ReturnState", returnState);
        
        NSString *nsState = [NSString stringWithCString:*returnState encoding:NSUTF8StringEncoding];
        NSLog(@"reture state = %@",nsState);
    }
    
}

/**
 *  设置当前的remoteControlServer
 *
 *  @param uuid remoteControlServer的uuid
 *
 *  @return 成功返回YES
 */

-(BOOL)setCurRCS:(NSString *)uuid{
    PLT_SkRemouteControler *skRC = getMediaRemoteControler();
    NPT_String result =  skRC->setCurRCS([uuid cStringUsingEncoding:NSUTF8StringEncoding],0);
    if (strcmp(result, "false")==0) {
        return NO;
    }
 
    NSLog(@"setCurRCS--SUCCESS");
    return YES;
}


-(void)setInputDevicesStates{
    NSString *split = @"\n\r";

    NSString *result = [NSString stringWithFormat:@"KeyBoard%@ON%@TouchScreen%@ON%@Mouse%@ON",split,split,split,split,split];
    PLT_SkRemouteControler *skRC = getMediaRemoteControler();
    skRC->setDeviceInputStates([result cStringUsingEncoding:NSUTF8StringEncoding]);
    
}

/**
 *  设置遥控器事件
 *
 *  @param event 事件
 */
-(void)setTouchScreenEvent:(TouchScreenEvent)event{
    PLT_SkRemouteControler *skRC = getMediaRemoteControler();
    NSString *eventData = [DlnaKeyboardEventData getKeyCodeEvent:event State:KEY_STATE_DOWN_AND_UP Time:[[NSDate date] timeIntervalSince1970]];
    skRC->setTouchScreenEvent([eventData cStringUsingEncoding:NSUTF8StringEncoding]);

}
/**
 *  设置屏幕点击事件
 *
 *  @param point 触点
 *  @param size  屏幕的尺寸
 */
-(void)setTouchScreenEventWithPoint:(CGPoint)point ScreenSize:(CGSize)size{
    CGRect rect = [self.touchScreenBoundaryValue CGRectValue];
    NSString *eventData = [DlnaKeyboardEventData getTouchEventWithPoint:point ScreenBoundary:rect fingerNum:1 ScreenSize:size];
    PLT_SkRemouteControler *skRC = getMediaRemoteControler();
    skRC->setTouchScreenEvent([eventData cStringUsingEncoding:NSUTF8StringEncoding]);
}
/**
 *  获取盒子屏幕的尺寸
 */
-(void)tryGetTouchScreenBoundary{
    PLT_SkRemouteControler *skRC = getMediaRemoteControler();
    skRC->tryGetTouchScreenBoundary();
}




@end
